package com.ruoyi.zyc.ts.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.zyc.ts.domain.Ts;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.zyc.ts.service.ITsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 天数Controller
 *
 * @author huacai
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/zyc/ts")
public class TsController extends BaseController
{
    @Autowired
    private ITsService tsService;

    /**
     * 查询天数列表
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ts ts)
    {
        startPage();
        List<Ts> list = tsService.selectTsList(ts);
        return getDataTable(list);
    }

    /**
     * 导出天数列表
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:export')")
    @Log(title = "天数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ts ts)
    {
        List<Ts> list = tsService.selectTsList(ts);
        ExcelUtil<Ts> util = new ExcelUtil<Ts>(Ts.class);
        util.exportExcel(response, list, "天数数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Ts> util = new ExcelUtil<Ts>(Ts.class);
        util.importTemplateExcel(response, "天数数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "天数", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zyc:ts:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Ts> util = new ExcelUtil<Ts>(Ts.class);
        InputStream inputStream = file.getInputStream();
        List<Ts> list = util.importExcel(inputStream );
        inputStream.close();
        int count = tsService.batchInsertTs(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取天数详细信息
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:query')")
    @GetMapping(value = "/{tsId}")
    public AjaxResult getInfo(@PathVariable("tsId") Integer tsId)
    {
        return success(tsService.selectTsByTsId(tsId));
    }

    /**
     * 新增天数
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:add')")
    @Log(title = "天数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ts ts)
    {
        return toAjax(tsService.insertTs(ts));
    }

    /**
     * 修改天数
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:edit')")
    @Log(title = "天数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ts ts)
    {
        return toAjax(tsService.updateTs(ts));
    }

    /**
     * 删除天数
     */
    @PreAuthorize("@ss.hasPermi('zyc:ts:remove')")
    @Log(title = "天数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tsIds}")
    public AjaxResult remove(@PathVariable Integer[] tsIds)
    {
        return toAjax(tsService.deleteTsByTsIds(tsIds));
    }
}
